package com.hmsm.sys.action;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.servlet.ModelAndView;

import com.hmsm.server.base.BaseAction;
import com.hmsm.server.common.CommonUtil;
import com.hmsm.sys.controller.LoginController;
import com.hmsm.sys.po.UserInfo;

public class LoginAction extends BaseAction<UserInfo> {
	
	/**
	 * @since Ver 1.1
	 */
	
	private static final long serialVersionUID = 4224453166598324314L;

	@Resource(name="loginController")
	public void setLoginController(LoginController loginController){
		setBaseController(loginController);
	}
	
	public LoginController getLoginController() {
		return (LoginController) getBaseController();
	}

	private String systemName;
	
	public String getSystemName() {
		return systemName;
	}

	//用户账号(系统使用用户账号登录)
	private String userName;

	//用户姓名
	private String name;
	private String password;
	private String veriCode;
	private int type;
	private String message;
	private String parAction;

	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getVeriCode() {
		return veriCode;
	}
	public void setVeriCode(String veriCode) {
		this.veriCode = veriCode;
	}
	public void setSyncData(String syncData) {
		this.veriCode = syncData;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getParAction() {
		return parAction;
	}

	public void setParAction(String parAction) {
		this.parAction = parAction;
	}

	private String code;
	
	public void setCode(String code) {
		this.code = code;
	}
	
	private String state;
	
	public void setState(String state) {
		this.state = state;
	}

	private String loginUrl;
	
	private String appId;
	
	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	private String agentId;

	/**
	 * @return the agentId
	 */
	public String getAgentId() {
		return agentId;
	}

	/**
	 * @param agentId the agentId to set
	 */
	public void setAgentId(String agentId) {
		this.agentId = agentId;
	}

	private String corpId;
	
	public String getCorpId() {
		return corpId;
	}
	
	private Long timeStamp;

	public Long getTimeStamp() {
		return timeStamp;
	}
	
	private String nonceStr;
	
	public String getNonceStr() {
		return nonceStr;
	}

	private String signature;

	public String getSignature() {
		return signature;
	}
	
	private String loginTmpCode;

	public String getLoginTmpCode() {
		return loginTmpCode;
	}

	public void setLoginTmpCode(String loginTmpCode) {
		this.loginTmpCode = loginTmpCode;
	}
	
	private String gotoUrl; 
	
	public String getGotoUrl(){
		String result  = "";
		
		if(gotoUrl!=null){
			try {
				result = URLEncoder.encode(gotoUrl, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				result = ""+e;
			}
		}
		
		return result;
	}
	
	public String getLoginUrl(){
		String url = "";

		if(loginUrl!=null){
			try {
				url = URLEncoder.encode(loginUrl, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				url = ""+e;
			}
		}
		
		return url;
	}
	
	public LoginAction(){
		systemName = (String) getSession().get("systemName");
		if(systemName==null){
			systemName = CommonUtil.getSysConfigMap().get("systemName");
			if(systemName!=null){
				getSession().put("systemName", systemName);
			}
		}
	}

	//登录
	public String login(){
		String viewName;
		
		ModelAndView modelAndView = getLoginController().login(userName, password, veriCode, code, state, getRequest());
		appId = (String) modelAndView.getModel().get("appId");
		loginUrl = (String) modelAndView.getModel().get("loginUrl");
		gotoUrl = (String) modelAndView.getModel().get("gotoUrl");
		corpId = (String) modelAndView.getModel().get("corpId");
		agentId = (String) modelAndView.getModel().get("agentId");
		timeStamp = (Long) modelAndView.getModel().get("timeStamp");
		nonceStr = (String) modelAndView.getModel().get("nonceStr");
		signature = (String) modelAndView.getModel().get("signature");
		message = (String) modelAndView.getModel().get("message");
		viewName = modelAndView.getViewName();
		
		return viewName;
	}
	
	/**
	 * 统一登录
	 * @return
	 * @author 舒俊
	 * 2013-10-22 下午01:50:47
	 * return:
	 */
	public String rhsLogin(){
		UserInfo currentUser = getSessionUser();
		if(currentUser==null){
			setMessage("RTX登录失败，请重新从腾讯通客户端登录后进入！");
			this.logError(this,getMessage());
			addActionError(getMessage());
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String rtxLogin(){
		boolean result = false;
		
		String accountSource = "RTX_001";
	
		UserInfo userInfo = null;
		try {
			userInfo = getUserInfoService().login(accountSource, userName, password);
		} catch (Exception e) {
			e.printStackTrace();
			mapResult.put("errorMessage", "腾讯通登陆发生错误："+e.getMessage());
		}
	
		if (userInfo==null) {
			result = false;
			mapResult.put("errorMessage", "腾讯通用户未登陆!");
		} else {
			getLoginController().setCurrentUser(userInfo, getRequest());
			// 基本可以确定登录成功
			result = true;
		}
		
		mapResult.put("success", result);
		return "json";
	}
	
	public String validateRtxUser(){
		boolean result = false;
		
		String accountSource = "RTX_001";
	
		UserInfo userInfo = null;
		try {
			userInfo = getUserInfoService().login(accountSource, userName, password);
		} catch (Exception e) {
			e.printStackTrace();
			mapResult.put("errorMessage", "腾讯通登陆发生错误："+e.getMessage());
		}
	
		if (userInfo==null) {
			result = false;
			mapResult.put("errorMessage", "腾讯通用户未登陆!");
		} else {
			// 基本可以确定登录成功
			Map<String, Object> user = new HashMap<String, Object>();
			user.put("userName",userInfo.getUserName());
			mapResult.put("user", user);
			result = true;
		}
		
		mapResult.put("success", result);
		return "json";
	}
	
	/**
	 * 前端提交登录临时代码，已废弃
	 * @return
	 */
	@Deprecated
	public String loginTmpCode(){
		mapResult = getLoginController().loginTmpCode(loginTmpCode, getRequest());
		return "json";
	}
	
	//管理员退出系统
	public String exit(){
		UserInfo currentUser =  this.getSessionUser();
		getLoginController().logout(currentUser, getRequest());

		return SUCCESS;
	}
	
}
